package com.colin.app.activity.method.photo;

import android.content.Intent;
import android.content.res.Configuration;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.view.View;
import android.widget.ImageView;

import com.colin.app.R;
import com.colin.app.activity.method.MethodContract;
import com.colin.app.activity.method.MethodPresenterImpl;
import com.colin.app.app.AppActivity;
import com.colin.app.bean.DemoBean;
import com.colin.library.help.ImageUtil;
import com.colin.library.other.Constants;

import java.io.File;

public class ChoosePhotoActivity extends AppActivity implements MethodContract.MethodView {

    private MethodContract.MethodPresenter mMethodPresenter = null;
    private ImageView image_photo_add;
    private ImageView image_photo_image_util;

    @Override
    protected void onDestroy() {
        if (null != mMethodPresenter) {
            mMethodPresenter.destroy();
            mMethodPresenter = null;
        }
        super.onDestroy();
    }

    @Override
    public void initPresenter() {
        DemoBean demoBean = getIntent().getExtras().getParcelable("clazz");
        mMethodPresenter = new MethodPresenterImpl(this, this, demoBean);
    }


    @Override
    public int initViewLayoutID() {
        return R.layout.activity_choose_photo;
    }

    @Override
    public void initView() {
        this.image_photo_add = (ImageView) this.findViewById(R.id.image_photo_add);
        this.image_photo_image_util = (ImageView) this.findViewById(R.id.image_photo_image_util);
    }

    @Override
    public void initListener() {
        this.image_photo_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mMethodPresenter.choosePhoto();
            }
        });
        this.image_photo_image_util.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ImageUtil.getInstance().showDialog(ChoosePhotoActivity.this, true, true, new ImageUtil.OnChooseImage() {
                    @Override
                    public void image(boolean success, File file, Object object) {
                        if (null != file) {
                            image_photo_image_util.setImageBitmap(BitmapFactory.decodeFile(file.getAbsolutePath()));
                        } else {
                            toastShow(object.toString());
                        }
                    }
                });
            }
        });

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (RESULT_OK != resultCode) {
            return;
        }
        if (requestCode > 10000) {
            ImageUtil.getInstance().onActivityResult(requestCode, resultCode, data);
        }
        switch (requestCode) {
            case Constants.REQUEST_CAMERA:
                mMethodPresenter.cropPhoto();
                break;
            case Constants.REQUEST_ALBUM:
                if (!mMethodPresenter.photoFileCreateSucceed()) {
                    return;
                }
                Uri uri = data.getData();
                if (uri != null) {
                    mMethodPresenter.cropPhoto(uri);
                }
                break;
            case Constants.REQUEST_CROP:
                image_photo_add.setImageURI(Uri.fromFile(mMethodPresenter.getPhotoFile()));
                break;
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode > 10000) {
            ImageUtil.getInstance().onRequestPermissionsResult(requestCode, permissions, grantResults);
        }
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
    }
}
